# -*- coding: utf-8 -*-
# @Time : 2025/6/23 下午2:14
# @Author : cc 
# @Email : liuchenchen@starmerx.com
# @Project : my-tools
# @Desc : 功能描述
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5.QtGui import QPainter, QColor, QImage, QPen
from PyQt5.QtCore import Qt


class CompositeDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QPainter复合模式演示")
        self.resize(600, 400)
        self.mode = QPainter.CompositionMode_SourceOver

    def paintEvent(self, event):
        painter = QPainter(self)

        # 绘制背景渐变
        painter.fillRect(self.rect(), QColor(100, 100, 150))

        # 绘制目标图形(红色半圆)
        painter.setPen(Qt.NoPen)
        painter.setBrush(QColor(255, 0, 0, 200))
        painter.drawEllipse(100, 100, 200, 200)

        # 设置复合模式
        painter.setCompositionMode(self.mode)

        # 绘制源图形(蓝色半圆)
        painter.setBrush(QColor(0, 0, 255, 150))
        painter.drawEllipse(200, 100, 200, 300)

    def setMode(self, mode):
        self.mode = mode
        self.update()


if __name__ == "__main__":
    app = QApplication([])
    window = CompositeDemo()
    window.show()

    # 可以动态更改模式观察效果
    # window.setMode(QPainter.CompositionMode_Destination)

    app.exec_()